package com.qzsoft.zhjd.asphalt.modules.asphalthomepage.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.system.modules.sysstation.entity.SysStationEntity;
import com.qzsoft.system.modules.sysstation.mapper.SysStationMapper;
import com.qzsoft.system.modules.sysstation.service.SysStationService;
import com.qzsoft.zhjd.asphalt.modules.asphalthomepage.mapper.HomePageMapper;
import com.qzsoft.zhjd.asphalt.modules.asphalthomepage.service.HomePageService;
import com.qzsoft.zhjd.asphalt.modules.asphaltdata.entity.AsphaltDataEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltdata.service.AsphaltDataService;
import com.qzsoft.zhjd.asphalt.modules.asphaltdatasieve.entity.AsphaltDataSieveEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltdatasieve.service.AsphaltDataSieveService;
import com.qzsoft.zhjd.asphalt.modules.asphalthomepage.dto.ProductionDataBaseDTO;
import com.qzsoft.zhjd.asphalt.modules.asphalthomepage.dto.AsphaltDataWeightDTO;
import com.qzsoft.zhjd.asphalt.modules.asphalthomepage.dto.AsphaltMonthAndYearCountDto;
import com.qzsoft.zhjd.asphalt.modules.asphalthomepage.dto.AsphaltMonthCountDto;
import com.qzsoft.zhjd.asphalt.modules.asphalthomepage.dto.GetCostAdjustmentDTO;
import com.qzsoft.zhjd.asphalt.modules.asphalthomepage.dto.OmitDepthOutputAtEachBaseDTO;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchThresholdEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchThresholdService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HomePageServiceImpl implements HomePageService {
    @Autowired
    private HomePageMapper homePageMapper;

    @Autowired
    private AsphaltMatchThresholdService asphaltMatchThresholdService;

    @Autowired
    private AsphaltMatchService asphaltMatchService;

    @Autowired
    private SysStationService sysStationService;

    @Autowired
    private AsphaltDataSieveService asphaltDataSieveService;

    @Autowired
    private AsphaltDataService asphaltDataService;

    @Autowired
    private SysStationMapper sysStationMapper;

    @Override
    public BigDecimal countByYear(String baseCode, String year) {
        return homePageMapper.countByYear(baseCode, year);
    }

    @Override
    public List<AsphaltMonthCountDto> countForMonth(String baseCode, String year) {
        return homePageMapper.countForMonth(baseCode, year);
    }

    /**
     * 产量统计
     *
     * @param params
     * @return
     */
    @Override
    public OmitDepthOutputAtEachBaseDTO omitDepthOutputAtEachBase(Map<String, Object> params) {
        if (null == params.get("year") && ObjectUtils.isEmpty(params.get("year"))) {
            return null;
        }
        Date current_time = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(current_time);
        String month = today.substring(5, today.lastIndexOf("-"));
        String day = today.substring(today.lastIndexOf("-"));
        params.put("month", params.get("year").toString() + "-" + month);
        params.put("day", params.get("year").toString() + "-" + month + "-" + day);
        // 获取产量和批次
        OmitDepthOutputAtEachBaseDTO PRODUCTION_FORMULA = homePageMapper.getProduction(params);
        // 获取工时
        OmitDepthOutputAtEachBaseDTO endOfHours = homePageMapper.getEndOfHours(params);
        OmitDepthOutputAtEachBaseDTO dto = new OmitDepthOutputAtEachBaseDTO();
        dto.setYearWeight(PRODUCTION_FORMULA.getYearWeight());
        dto.setYearNumber(PRODUCTION_FORMULA.getYearNumber());
        dto.setYearEndOfHours(endOfHours.getYearEndOfHours());
        dto.setMonthWeight(PRODUCTION_FORMULA.getMonthWeight());
        dto.setMonthNumber(PRODUCTION_FORMULA.getMonthNumber());
        dto.setMonthEndOfHours(endOfHours.getMonthEndOfHours());
        dto.setDayWeight(PRODUCTION_FORMULA.getDayWeight());
        dto.setDayNumber(PRODUCTION_FORMULA.getDayNumber());
        dto.setDayEndOfHours(endOfHours.getDayEndOfHours());
        return dto;
    }

    /**
     * 材料成本核算
     *
     * @param params
     * @return
     */
    @Override
    public List<GetCostAdjustmentDTO> getCostAdjustment(Map<String, Object> params) {
        String reportType = ObjectUtils.isEmpty(params.get("reportType")) ? "all" : params.get("reportType").toString();
        params.put("reportType", reportType);
        List<GetCostAdjustmentDTO> costAdjustment = homePageMapper.getCostAdjustment(params);
        return costAdjustment;
    }

    /**
     * 产量情况
     *
     * @param params
     * @return
     */
    @Override
    public List<JSONObject> plantProduction(Map<String, Object> params) {
        String reportType = ObjectUtils.isEmpty(params.get("reportType")) ? "all" : params.get("reportType").toString();
        params.put("reportType", reportType);
        List<JSONObject> plantProduction = homePageMapper.getPlantProduction(params);
        if ("day".equals(reportType)) {
            plantProduction.sort((ord1, ord2) -> String.valueOf(ord1.get("dischargeTime"))
                    .compareTo(String.valueOf(ord2.get("dischargeTime"))));
        }
        return plantProduction;
    }

    /**
     * 项目供应情况
     *
     * @param params
     * @return
     */
    @Override
    public List<JSONObject> estimatedTimeOfDelivery(Map<String, Object> params) {
        return homePageMapper.getEstimatedTimeOfDelivery(params);
    }

    /**
     * 实时生产情况
     *
     * @param params
     * @return
     */
    @Override
    public List<AsphaltDataWeightDTO> realTimeProduction(Map<String, Object> params) {
        return homePageMapper.getRealTimeProduction(params);
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public ProductionDataBaseDTO details(String id) {
        // 获取数据
        AsphaltDataEntity data = asphaltDataService.getById(id);
        if (null == data) {
            return null;
        }

        // 获取筛孔数据
        AsphaltDataSieveEntity sieveData = asphaltDataSieveService.getById(id);

        // 获取拌和站信息
        String stationId = data.getStationCode();
        SysStationEntity station = sysStationService.getOne(new QueryWrapper<SysStationEntity>().eq("code", stationId));

        // 获取配比信息
        String matchCode = data.getMatchCode();
        AsphaltMatchEntity match = asphaltMatchService.getByCode(matchCode);
        AsphaltMatchThresholdEntity threshold = null;
        if (null != match) {
            String matchName = match.getMatchName();
            threshold = asphaltMatchThresholdService.getByMatchName(matchName);
        }

        // 返回数据
        ProductionDataBaseDTO dto = new ProductionDataBaseDTO();
        dto.setData(data);
        dto.setThreshold(threshold);
        dto.setStation(station);
        dto.setMatch(match);
        dto.setSieveData(sieveData);
        return dto;
    }

    /**
     * 根据基地获取拌合楼
     *
     * @param params
     * @return
     */
    @Override
    public List<SysStationEntity> getTokenStart(Map<String, Object> params) {
        QueryWrapper<SysStationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("base_code", params.get("baseCode"));
        // queryWrapper.eq("type", "asphalt");
        queryWrapper.orderByAsc("code");
        List<SysStationEntity> stationEntities = sysStationMapper.selectList(queryWrapper);
        return stationEntities;
    }

    @Override
    public AsphaltMonthAndYearCountDto getCountByMonthAndYear(String baseCode, String month, String year) {
        Date date = new Date();
        SimpleDateFormat sdf = null;
        if (ObjectUtils.isEmpty(year)) {
            sdf = new SimpleDateFormat("yyyy");
            year = sdf.format(date);
        }
        if (ObjectUtils.isEmpty(month)) {
            sdf = new SimpleDateFormat("yyyy-MM");
            month = sdf.format(date);
        }
        AsphaltMonthAndYearCountDto dto = homePageMapper.getCountByMonthAndYear(baseCode, month, year);
        if(null == dto) {
            dto = new AsphaltMonthAndYearCountDto();
            dto.setMonthWeight(BigDecimal.ZERO);
            dto.setYearWeight(BigDecimal.ZERO);
        }
        dto.setMonth(month);
        dto.setYear(year);
        return dto;
    }

}
